/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */

package com.gadglet.gadgets.discussions.client;

import java.util.HashMap;

import com.gadglet.client.gwt.GadgetNativeUtils;
import com.gadglet.client.gwt.GadgetUtils;
import com.gadglet.client.gwt.core.GadgletQuery;
import com.gadglet.client.gwt.core.GadgletRequest;
import com.gadglet.client.gwt.core.GadgletResponse;
import com.gadglet.client.gwt.ui.ActionLabel;
import com.gadglet.client.gwt.ui.ContentText;
import com.gadglet.client.gwt.ui.DebugDialogBox;
import com.gadglet.client.gwt.ui.ProfileViewMainPanel;
import com.gadglet.client.gwt.ui.SimpleDialogBox;
import com.gadglet.gadgets.discussions.shared.Params;
import com.gadglet.params.ReqActionTypes;
import com.gadglet.params.ReqTypes;
import com.gadglet.params.SortTypes;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gadgets.client.ContentSection;
import com.google.gwt.gadgets.client.Gadget.ContentType;
import com.google.gwt.gadgets.client.Gadget.InjectContent;
import com.google.gwt.gadgets.client.PreferencesFeature;
import com.google.gwt.gadgets.client.PreferencesProvider;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
/**
 * The Gadget Profile view 
 * <br>
 * This view is used in Gmail Side bar and enables to :
 * <li> view items
 * <li> Navigate to Canvas view
 *
 */
@InjectContent(files = "home.html")
@ContentType(type = "html", views = {"profile"})
public class DiscussionsProfileView extends ContentSection<DiscussionsGadget>{

	
	private DebugDialogBox debug = null;

	final GadgletQuery viewDiscussionQry = new GadgletQuery(
			ReqTypes.BIZLET_SIGNED);
	final ViewDiscussions viewDiscussionRequest = new ViewDiscussions(
			viewDiscussionQry);
	
	
		
	final Grid results = new Grid();
	protected final HorizontalPanel paging = new HorizontalPanel();
	
	
	final Button prevButton = new Button();
	final Button nextButton = new Button();
	final PreferencesFeature  prefsUtils = PreferencesProvider.get();
	final SimpleDialogBox serverMessage = SimpleDialogBox.getMesseageDialogBox();
	
	

	@Override
	public  void init(DiscussionsGadget gadget ) {
			
	
				
		Image expand = new Image(GadgetUtils.getIconURL("expand",
				null, false));
		expand.setTitle(prefsUtils.getMsg("gadgetLabelgadgetLabelExpand"));
		expand.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
			
				try {
					GadgetNativeUtils.requestNavigateToCanvas();
				} catch (Exception e) {
					serverMessage.showError( e.getMessage());
				}
			}
		});
		
		debug = DebugDialogBox.getErrorNotifier();
				
		prevButton.setText(prefsUtils.getMsg("gadgetLabelPrevious"));
		nextButton.setText(prefsUtils.getMsg("gadgetLabelNext"));
		
		final ProfileViewMainPanel mainPanel = new ProfileViewMainPanel();
		
		mainPanel.getHeader().add(expand);
		
				
		mainPanel.add(results);
		
		RootPanel.get().add(mainPanel.getGadgetPanel());
					
		
		viewDiscussionQry.setRequestAction(ReqActionTypes.VIEW);
		viewDiscussionQry.addSortBy("gadgetCreation", SortTypes.ASC);
	
		viewDiscussionQry.setStartItemsFrom(0);
		viewDiscussionQry.setItemsNum(5);
		viewDiscussionRequest.makeRequest();
		
		prevButton.setVisible(false);
		prevButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				viewDiscussionQry.setStartItemsFrom(viewDiscussionQry
						.getStartFrom()
						- viewDiscussionQry.getItemNum());
				viewDiscussionRequest.makeRequest();

			}

		});
		paging.add(prevButton);

		
		nextButton.setVisible(false);
		nextButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				viewDiscussionQry.setStartItemsFrom(viewDiscussionQry
						.getStartFrom()
						+ viewDiscussionQry.getItemNum());
				viewDiscussionRequest.makeRequest();

			}

		});
		paging.add(nextButton);


	}

	public class ViewDiscussions extends GadgletRequest {
		GadgletResponse jResponse;
		ViewDiscussions(GadgletQuery req) {
			super(req);
	
		}

		@Override
		protected void processResults(GadgletResponse data) {
			 jResponse = data;
			 
			if (jResponse.isSuccessful()) {
				
				results.resize(1, 4);
				results.clear();
				int index;
			
				for (index = 0; index < jResponse.getRootItemsNum(); index++){
				
					ContentText text = new ContentText(data.getStringItemValue(
							Params.DISCUSSION_TEXT.getParamName(), index));
					final String discussionId = jResponse.getItemKeyValue(index);
					final String title = data.getStringItemValue(
							Params.DISCUSSION_TEXT.getParamName(), index);
						
					results.setWidget(index, 0, text);
					
					final ActionLabel viewLabel = new ActionLabel(prefsUtils.getMsg("gadgetLabelView"));

					viewLabel.addClickHandler(new ClickHandler() {
						@Override
						public void onClick(ClickEvent event) {
							
							
							HashMap <String,String> params = new HashMap <String,String> ();
							params.put(Params.DISCUSSION_ID.getParamName(), discussionId);
							
							params.put(Params.DISCUSSION_TEXT.getParamName(), title);
							
							GadgetNativeUtils.requestNavigateToCanvas(GadgetUtils.createJsonParamList(params));
							
							
							
						}

					});
					
					results.setWidget(index, 1, viewLabel);
					
							
					results.resize(index+3, 4);
					
				}

				if (viewDiscussionQry.getStartFrom() > 0)
					prevButton.setVisible(true);
				else
					prevButton.setVisible(false);

				if (jResponse.hasMoreItems())
					nextButton.setVisible(true);
				else
					nextButton.setVisible(false);
				
				results.setWidget(index, 0, paging);
			
			}

			else {
				debug.showError(1, jResponse.getError());
			}

		}
	}
	
}
